package com.ebeitech.library.ui;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ebeitech.library.R;
import com.ebeitech.library.R2;
import com.ebeitech.library.util.StringUtil;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by alice on 2017/10/31.
 */
public class NumberVariateView extends LinearLayout {

	@BindView(R2.id.btn_minus)
	ImageView btnMinus;
	@BindView(R2.id.et_number)
	EditText mNumberEdit;
	@BindView(R2.id.btn_add)
	ImageView btnAdd;

	private Context mContext;

	private boolean isDataSeting = false;
	private OnNumberVariateListener mNumberVariateListener;
	private int border = 1;// 限制的最小值

	public void setOnNumberVariateListener(OnNumberVariateListener l) {
		mNumberVariateListener = l;
	}

	public void setBorder(int border) {
		this.border = border;
	}

	/**
	 * 设置最大的长度
	 *
	 * @param length
	 */
	public void setMaxLength(int length) {
		mNumberEdit.setFilters(new InputFilter[]{
				new InputFilter.LengthFilter(length)});
	}

	public float getNumber() {
		String numberStr = mNumberEdit.getText().toString().trim();
		float number = 0;
		if (!StringUtil.isEmpty(numberStr)) {
			try {
				number = Float.parseFloat(numberStr);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return number;
	}

	public void setNumber(float number) {
		isDataSeting = true;
		mNumberEdit.setText(StringUtil.formatWithDecimal(number + "", 2) + "");
	}

	public NumberVariateView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;
		ininView();
	}

	private void ininView() {
		// TODO Auto-generated method stub
		View view = LayoutInflater.from(mContext).inflate(R.layout.ebei_view_number_variate, null);
		addView(view);
		ButterKnife.bind(this);

		mNumberEdit.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				if (s.toString().contains(".")) {
					if (s.length() - 1 - s.toString().indexOf(".") > 2) {
						s = s.toString().subSequence(0,
								s.toString().indexOf(".") + 3);
						mNumberEdit.setText(s);
						mNumberEdit.setSelection(s.length());
					}
				}
				if (s.toString().trim().substring(0).equals(".")) {
					s = "0" + s;
					mNumberEdit.setText(s);
					mNumberEdit.setSelection(2);
				}
				if (s.toString().startsWith("0")
						&& s.toString().trim().length() > 1) {
					if (!s.toString().substring(1, 2).equals(".")) {
						mNumberEdit.setText(s.subSequence(0, 1));
						mNumberEdit.setSelection(1);
						return;
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
										  int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				if (isDataSeting) {
					isDataSeting = false;
					return;
				}
				String numberStr = mNumberEdit.getText().toString();
				float number = 0;
				if (!StringUtil.isEmpty(numberStr.trim())) {
					try {
						number = Float.parseFloat(numberStr);
					} catch (Exception e) {
						number = 0;
						e.printStackTrace();
					}
				}
				if (mNumberVariateListener != null) {
					mNumberVariateListener.onVariate(get2Decimal(number));
				}
			}
		});
	}

	@OnClick(R2.id.btn_minus)
	void onMinusClick(View v) {
		String numberStr = mNumberEdit.getText().toString();
		float number = 0;
		try {
			number = Float.parseFloat(numberStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		number--;
		if (number >= border) {
			if (number == (int)number)
			{
				mNumberEdit.setText((int)number + "");
			}
			else {
				mNumberEdit.setText(get2Decimal(number) + "");
			}

		}
		if (mNumberVariateListener != null) {
			mNumberVariateListener.onMinus(number < border);
		}
	}

	@OnClick(R2.id.btn_add)
	void onAddClick(View v) {
		String numberStr = mNumberEdit.getText().toString();
		float number = 0;
		try {
			number = Float.parseFloat(numberStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		number++;
		if (number > 0) {
			if (number == (int)number)
			{
				mNumberEdit.setText((int)number + "");
			}
			else {
				mNumberEdit.setText(get2Decimal(number) + "");
			}

		}
		if (mNumberVariateListener != null) {
			mNumberVariateListener.onAdd();
		}
	}

	private float get2Decimal(float number)
	{
		BigDecimal bg = new BigDecimal(number);
		float f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
		return f1;
	}

	public interface OnNumberVariateListener {
		void onAdd();

		void onMinus(boolean isBorder);

		void onVariate(float number);
	}
}
